package com.jky.stmall.front.service.impl;

import com.jky.stmall.front.entity.Cart;
import com.jky.stmall.front.entity.Goods;
import com.jky.stmall.front.mapper.CartMapper;
import com.jky.stmall.front.service.CartService;
import com.jky.stmall.front.service.GoodsService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@AllArgsConstructor
public class CartServiceImpl implements CartService {
    private CartMapper cartMapper;
    private GoodsService goodsService;

    @Override
    public int add(Cart cart) throws Exception{
        //验证
        //goodsId(商品上下架问题)
        Goods goods=goodsService.getById(cart.getGoodsId());
        if(goods ==null){
            throw new Exception("商品ID不存在");
        }
        if (goods.getStatus()==1){
            throw new Exception("商品已下架，无法加入购物车");
        }
        //count：1
       //如果当前用户的购物车中，已经存在了当前商品 不新增 在原来的 基础上进行数量增加
        Cart sCart =cartMapper.selectByGoodsIdAndUserId(cart.getGoodsId(),cart.getUserId());
        if (sCart==null){
            //count：1
            if (goods.getCount()<1){
                throw new Exception("库存不足，无法加入购物车");
            }
            return cartMapper.insert(cart);
        }else {
            //如果已经存在一条购物车  只进行数量的增加
            sCart.setCount(sCart.getCount()+1);
            if (goods.getCount()< sCart.getCount()){
                throw new Exception("库存不足，无法加入购物车");
            }
            //调用修改操作
            return  cartMapper.update(sCart);
        }
    }


    @Override
    public int remove(Integer id,Integer userId) throws Exception {
        //根据ID查询购物车信息
        Cart cart=cartMapper.selectById(id);
        if (cart.getUserId()!=userId){
            throw new Exception("权限不足无法删除");
        }
        return cartMapper.delete(id);
    }

    @Override
    public int edit(Cart cart, Integer userId) throws Exception {
        //验证用户id 是否正确
        Cart sCart=cartMapper.selectById(cart.getId());
        if (sCart.getUserId()!=userId){
            throw  new Exception("权限不足,无法删除");
        }

        //商品下架
        Goods goods=goodsService.getById(sCart.getGoodsId());

        if (goods.getStatus()==1){
            //删除购物车信息
            remove(sCart.getId(),userId);
            //修改失败
            throw new Exception("商品已经下架，无法修改购物车");

        }
        //验证商品库存不足
        if(goods.getCount()<cart.getCount()){
            cart.setCount(cart.getCount());
        }
        return  cartMapper.update(cart);
    }

    @Override
    public List<Cart> getByUserId(Integer userId) {
        return cartMapper.selectByUserId(userId);
    }

    @Override
    public List<Cart> getByIds(Integer[] cartIds, Integer userid) throws Exception {
        if (cartIds==null||cartIds.length==0){
            throw new Exception("商品列表不能为空！！！");
        }
        List<Cart> carts= cartMapper.selectByIds(cartIds);
        //有些购物车的id 是无效的
        if (carts.size()<cartIds.length){
            throw  new Exception("购物车中包含无效ID，有的商品已经被结算！！");
        }

        for (Cart cart:carts) {
            if (cart.getUserId()!=userid){
                throw new Exception("登录信息已更改，请重新请求");
            }
        }
        return carts;
    }
}
